import Request from "@/lib/luch-request/index.js";
import {baseURL} from "./app"

export const getTokenStorage = () => {
    let token = "";
    try {
        token = uni.getStorageSync("token");
    } catch (e) {}
    return token;
};

const http = new Request();

/* 设置全局配置 */
http.setConfig((config) => {
  config.header = {
    ...config.header,
  };
  config.sslVerify = false
  config.baseURL = baseURL
  // 自定义的配置
  config.custom = {
    auth: true, // 是否传token
  };
  return config;
});

http.interceptors.request.use(
  async (config) => {
      config.header = {
        ...config.header,
          token:getTokenStorage()
      }
      return config;
    },
    (config) => {
      return Promise.reject(config);
    }
);

http.interceptors.response.use(
  (response) => {
    if (response.data.code === 401) {
     uni.showToast({
         icon:'none',
       title:response.data.msg
     })
	  uni.navigateTo({
		url: '/pages/login/login'
	  })
    }
	if (response.data.code === 0) {
	  uni.showToast({icon:'none',title:response.data.msg})
	}
	
    return response;
  },
  (response) => {
    /*  对响应错误做点什么 （statusCode !== 200）*/
    // console.log('对响应错误做点什么', response.data);
      if (response.data.code === 401) {
          uni.showToast({
              icon:'none',
              title:response.data.msg
          })
          uni.navigateTo({
              url: '/pages/login/login'
          })
      }

    return Promise.reject(response);
  }
);

export default http
